
package com.sskj.mq.consumer;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.amap.message.Message;
import com.sskj.jkzx.domain.entity.amap.message.YcAlertMessage;
import com.sskj.jkzx.domain.entity.amap.yc.YcMarkerInfo;
import com.sskj.jkzx.domain.entity.amap.yc.ro.EquipmentInfo;
import com.sskj.jkzx.domain.entity.amap.yc.ro.EquipmentInfoMore;
import com.sskj.jkzx.domain.entity.amap.yc.ro.MonitorData;
import com.sskj.jkzx.domain.entity.amap.yc.ro.ProjectInfo;
import com.sskj.jkzx.domain.entity.quantity.YCAlertQuantity;
import com.sskj.jkzx.repository.amap.message.MessageQueryServiceImpl;
import com.sskj.jkzx.service.impl.yc.YCCURDServiceImpl;

@Component
public class YcsbsssjMQConsumer {

	protected final Log logger = LogFactory.getLog(YcsbsssjMQConsumer.class);

	@Autowired
	private SimpMessagingTemplate template;

	@Autowired
	private YCCURDServiceImpl service;

	@Autowired
	private MessageQueryServiceImpl messageService;

	public final static SimpleDateFormat DATA_FORMAT_YYYYMMDDHHMMSS = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//	@JmsListener(destination = "yangchenyichangtopicmessage", containerFactory = "ycsbsssjListener")
	public void receiveTopic(String text) {

		if (text != null) {

			JSONObject resultJson = JSONObject.parseObject(text);
			if (resultJson == null) {
				return;
			}
			if (resultJson.getJSONObject("projectInfo") == null) {
				logger.info("Topic [扬尘设备实时数据]：" + text);
				return;
			}
			ProjectInfo projectInfo = JSON.parseObject(resultJson.getJSONObject("projectInfo").toJSONString(),
					ProjectInfo.class);

			if (projectInfo == null) {
				logger.info("Topic [扬尘设备实时数据]：" + text);
				return;
			}

			YcMarkerInfo ycMarkerInfo = new YcMarkerInfo();
			ycMarkerInfo.setId(projectInfo.getPcode());
			ycMarkerInfo.setLat(projectInfo.getLat());
			ycMarkerInfo.setLng(projectInfo.getLng());
			ycMarkerInfo.setName(projectInfo.getPname());

			ycMarkerInfo.setAlertCode(projectInfo.getColor());
			if ("1".equals(projectInfo.getColor())) {
				ycMarkerInfo.setAlertColor("green");
			} else if ("2".equals(projectInfo.getColor())) {
				ycMarkerInfo.setAlertColor("yellow");
			} else if ("3".equals(projectInfo.getColor())) {
				ycMarkerInfo.setAlertColor("red");
			} else if ("4".equals(projectInfo.getColor())) {
				ycMarkerInfo.setAlertColor("yellow");
			} else if ("5".equals(projectInfo.getColor())) {
				ycMarkerInfo.setAlertColor("red");
			} else {
				ycMarkerInfo.setAlertColor("green");
			}

			double pm2 = 0;
			double pm10 = 0;
			for (EquipmentInfo e : projectInfo.getList()) {
				BigDecimal v1 = new BigDecimal(e.getPmTen());
				BigDecimal v2 = new BigDecimal(pm10);
				if (v1.compareTo(v2) >= 0) {
					pm10 = e.getPmTen();
				}
				BigDecimal v3 = new BigDecimal(e.getPmTwo());
				BigDecimal v4 = new BigDecimal(pm2);
				if (v3.compareTo(v4) >= 0) {
					pm2 = e.getPmTwo();
				}
			}
			ycMarkerInfo.setPm2(new BigDecimal(pm2).setScale(1, BigDecimal.ROUND_HALF_UP).toString());
			ycMarkerInfo.setPm10(new BigDecimal(pm10).setScale(1, BigDecimal.ROUND_HALF_UP).toString());

			if (projectInfo.getList() != null) {
				ycMarkerInfo.setEis(projectInfo.getList());
			}
			if (resultJson.getJSONObject("esi") != null) {
				EquipmentInfoMore esi = JSON.parseObject(resultJson.getJSONObject("esi").toJSONString(),
						EquipmentInfoMore.class);
				if (esi != null) {
					ycMarkerInfo.setSprinklestatus(esi.getSprinklestatus());
					ycMarkerInfo.setFogcannonstatus(esi.getFogcannonstatus());
				}
			}
			JSONObject monitorJsonData = resultJson.getJSONObject("monitorData");
			if (monitorJsonData != null) {
				MonitorData md = monitorJsonData.toJavaObject(MonitorData.class);
				ycMarkerInfo.setMd(md);
			}

			template.convertAndSend("/topic/ycsj", ycMarkerInfo);

			if ("4".equals(projectInfo.getColor()) || "5".equals(projectInfo.getColor())) {
				// 如果颜色值为4或者5,则保存为预警消息
				String type;
				if ("4".equals(projectInfo.getColor())) {
					// 黄色预警
					type = "YA";
				} else if ("5".equals(projectInfo.getColor())) {
					// 红色预警
					type = "RA";
				} else {
					return;
				}
				Message message = new Message(type, text);
				message.setCreatedTime(new Date());
				messageService.saveMessage(message);

				YcAlertMessage result = new YcAlertMessage();
				result.setAlertColor(ycMarkerInfo.getAlertColor());
				result.setColorCode(projectInfo.getColor());
				result.setGlId(projectInfo.getPcode());
				result.setGlName(projectInfo.getPname());
				result.setLatitude(projectInfo.getLatGd());
				result.setLongitude(projectInfo.getLngGd());
				result.setObjId(String.valueOf(projectInfo.getId()));
				result.setPl(ycMarkerInfo.getSprinklestatus());
				result.setWp(ycMarkerInfo.getFogcannonstatus());
				result.setPmTen(ycMarkerInfo.getPm10());
				result.setPmTwo(ycMarkerInfo.getPm2());
				result.setTxms("扬尘监测预警");
				result.setMsglx(type);
				result.setType(type);
				result.setJssj(DATA_FORMAT_YYYYMMDDHHMMSS.format(message.getCreatedTime()));

				template.convertAndSend("/topic/ycAlertMessage", result);
			}
		}
	}

//	@JmsListener(destination = "yangchenyichangtopicalertstatistics", containerFactory = "ycyjtjListener")
	public void receiveYcyjtjTopic(String text) {

		logger.info("Topic [扬尘设备预警统计数据]：" + text);
		if (StringUtils.isNotBlank(text)) {
			JSONObject resultJson = JSONObject.parseObject(text);
			YCAlertQuantity entity = JSONObject.parseObject(text, YCAlertQuantity.class);
			if (entity != null) {
				service.saveAlertQuantity(entity);
			}
			logger.info(resultJson);
		}
	}

}
